#需要导入两个包 PyJWT和datetime
import jwt
#jwt  encode token编码 和decode token解码
import datetime
from django.conf import settings

class JWTTool:
     # 生成token
    def getToken(self,userid,account):
        #定义载荷数据
        data={
            "user_id":userid,
            "account":account,
            "exp":datetime.datetime.now() + datetime.timedelta(hours=1)
        }
        #自定义的密钥
        key = "AKIDSggmeI7z2qSUHoaf18zb4JKdZv61PEZf"
        # 生成token
        token = jwt.encode(payload=data, key=key, algorithm="HS256")
        # 将字节对象转换为字符串
        token_str = token.decode('utf-8')
        return token_str
    def checkToken(self,token):
        key = "AKIDSggmeI7z2qSUHoaf18zb4JKdZv61PEZf"
        return jwt.decode(token,key=key, algorithms="HS256")

tooljwt = JWTTool()
#print(tool.getToken(1,"admin"))
#print(tool.checkToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjEsImFjY291bnQiOiJhZG1pbiIsImV4cCI6MTczNDk1NDk4NH0.wDsrEEo0KcBXWz-p3y08Bh7pbDjbbgyYSlQ68mQM6G4'))